﻿using System;
using System.IO;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.DAL;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Prodamo.MostNeedFunctions;
using Jungle.Prodamo.PaySystem;
using Jungle.Prodamo.Core;

public partial class user : BaseUserModule
{
    SessionKeeper thisSession = new SessionKeeper();
    int id_user;
    protected override void OnInit(EventArgs e)
    {
        btn_SendCash.PostBackUrl = Request.RawUrl;

        thisSession.SessionId = Session.SessionID.ToString();
        thisSession.GetData();


        base.OnInit(e);
    }

    // Рендер метод
    protected override void Render(HtmlTextWriter writer)
    {
        if (thisSession.UserId != -1) GetStatusContent(writer);
        else Response.Redirect(UrlHelper.GetSiteUrl());

        base.Render(writer);
    }

    // Пользовательская функция генерирвания контента
    protected HtmlTextWriter GetStatusContent(HtmlTextWriter writer)
    {
        GetAccountMenu(writer);

        writer.AddAttribute(HtmlTextWriterAttribute.Id, "account_status");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_table");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        writer.Write("<strong>Мои счета</strong>");
        writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "auc_table");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

        writer.AddAttribute(HtmlTextWriterAttribute.Width, "300px");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("<strong>ID счета</strong>");
        writer.RenderEndTag();
        writer.AddAttribute(HtmlTextWriterAttribute.Width, "70px");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("<strong>Баланс</strong>");
        writer.RenderEndTag();

        writer.RenderEndTag();

        id_user = thisSession.UserId;
        using (SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnectionUserBase"].ConnectionString))
        {
            SqlCommand command = new SqlCommand("mdAccount_GetByUser", connect);
            SqlParameter param_id_user = command.Parameters.Add("@id_user", SqlDbType.Int);
            SqlDataReader reader = null;

            command.CommandType = CommandType.StoredProcedure;
            param_id_user.Value = id_user;

            command.Connection.Open();
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.AddAttribute(HtmlTextWriterAttribute.Width, "300px");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("<strong>" + reader.GetValue(0).ToString() + "</strong>");
                writer.RenderEndTag();
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "70px");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(reader.GetValue(2).ToString());
                writer.Write("&nbsp;");
                writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
                writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + "/Images/money.png");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.RenderEndTag();
            }
            command.Connection.Close();
        }
        writer.RenderEndTag();

        writer.RenderEndTag();
        GetHistoryContent(writer);
        return writer;
    }
    protected HtmlTextWriter GetHistoryContent(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Id, "account_history");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        writer.Write("<strong>История платежей</strong>");
        writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_table");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

        //writer.AddAttribute(HtmlTextWriterAttribute.Width, "300px");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("<strong>Дата и время</strong>");
        writer.RenderEndTag();
        //writer.AddAttribute(HtmlTextWriterAttribute.Width, "70px");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("<strong>Кто - кому</strong>");
        writer.RenderEndTag();
        //writer.AddAttribute(HtmlTextWriterAttribute.Width, "70px");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("<strong>Сумма</strong>");
        writer.RenderEndTag();

        writer.RenderEndTag();

        id_user = thisSession.UserId;
        using (SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnectionUserBase"].ConnectionString))
        {
            SqlCommand command = new SqlCommand("mdAccount_History_GetByUser", connect);
            SqlParameter param_id_user = command.Parameters.Add("@userId", SqlDbType.Int);
            SqlDataReader reader = null;

            command.CommandType = CommandType.StoredProcedure;
            param_id_user.Value = id_user;

            command.Connection.Open();
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(reader.GetValue(4).ToString());
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    if (reader.GetValue(1).ToString() == id_user.ToString())
                    {
                        writer.Write("Я&nbsp;");
                        writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + "/Images/next.png");
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                        UserManager um = new UserManager();
                        um.UserId = Convert.ToInt32(reader.GetValue(2));
                        um.GetById();

                        writer.Write("&nbsp;" + um.UserLink());
                    }
                    if (reader.GetValue(2).ToString() == id_user.ToString())
                    {
                        writer.Write("Мне&nbsp;");
                        writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + "/Images/back.png");
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                        UserManager um = new UserManager();
                        um.UserId = Convert.ToInt32(reader.GetValue(1));
                        um.GetById();
                        writer.Write("&nbsp;" + um.UserLink());                    
                    }
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(reader.GetValue(3).ToString()+ "&nbsp;");
                    writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + "/Images/money.png");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                writer.RenderEndTag();
            }
            command.Connection.Close();
        }
        writer.RenderEndTag();

        writer.RenderEndTag();
        return writer;
    }
    protected HtmlTextWriter GetAccountMenu(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.AddAttribute("style", "cursor: pointer;");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute("onclick", "AccountShow('1');");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("<strong>..:Баланс моего счета:..|</strong>");
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "AccountMenuBlock");
            writer.AddAttribute("onclick", "AccountShow('2');");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("<strong>..:Перевод денег:..|</strong>");
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "AccountMenuBlock");
            writer.AddAttribute("onclick", "AccountShow('3');");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("<strong>..:История платежей:..</strong>");
            writer.RenderEndTag();
        writer.RenderEndTag();
        writer.Write("<br/>");

        return writer;
    }

    protected void SendCash(object sender, EventArgs e)
    {
        ValidBlock.InnerHtml = "";
        Account acc = new Account();
        double sum = Convert.ToDouble(tb_Summ.Text);
        double accSum = acc.GetUserBalans(thisSession.UserId);
        int accId = Convert.ToInt32(tb_AccountId.Text);
        int from = acc.GetUserIdByAccountId(accId);


        if (accSum >= sum)
        {
            if (from != -1)
            {
                acc.TranzactInCash(thisSession.UserId, from, sum);
            }
            else
                ValidBlock.InnerHtml = "Такого счета не существует";
        }
        else
            ValidBlock.InnerHtml = "Недостаточно средств на счету";
    }
}
